﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model;
using BLL;

public partial class AddHouse : System.Web.UI.Page
{
    private int _userId;
    public int UserId
    {
        get { return _userId; }
        set { _userId = value; }
    }

    private int _userType;
    public int UserType
    {
        get { return _userType; }
        set { _userType = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        // Test Code
        Session["UserType"] = 0;
        Session["UserId"] = 2;
        Session["pId"] = 1004;

        // Normal
        AccountChecked();

        if (Session["UserType"] == null || Session["UserId"] == null || Int32.Parse(Session["UserType"].ToString()) != 0)
            Response.Redirect("~/Home.aspx");
        else
        {
            UserId = Int32.Parse(Session["UserId"].ToString());
            UserType = Int32.Parse(Session["UserType"].ToString());
        }
        if (IsPostBack)
            return;
        Bind();
        
    }

    private void Bind()
    {
        this.rblHouseType.DataSource = HouseTypeManage.GetAllHouseTypes();
        this.rblHouseType.DataTextField = "Name";
        this.rblHouseType.DataBind();
        this.rblHouseType.SelectedIndex = 0;

        this.rblLeaseMode.DataSource = LeaseModeManage.GetAllLeaseModes();
        this.rblLeaseMode.DataTextField = "Name";
        this.rblLeaseMode.DataBind();
        this.rblLeaseMode.SelectedIndex = 0;

        // 绑定地区列表
        this.ddlAreaList.DataSource = AreaManage.GetAllAreas();
        this.ddlAreaList.DataTextField = "Name";
        this.ddlAreaList.DataBind();
        this.ddlAreaList.SelectedIndex = 0;
    }

    protected void AddInfo(object sender, EventArgs e)
    {
        // 有空元素则失败
        if (tbAddress.Text == null || tbAddress.Text.Length == 0 ||
            tbDuration.Text == null || tbDuration.Text.Length == 0 ||
            tbExpirationDate.Text == null || tbExpirationDate.Text.Length == 0 ||
            tbIntakeTime.Text == null || tbIntakeTime.Text.Length == 0 ||
            tbPrice.Text == null || tbPrice.Text.Length == 0 ||
            tbRoomNumber.Text == null || tbRoomNumber.Text.Length == 0 ||
            tbStandard.Text == null || tbStandard.Text.Length == 0 ||
            !fuldLargeImage.HasFile ||
            !fuldThumbnail.HasFile)
        {
            this.Tip.Text = "元素不能为空，添加失败";
            return;
        }
            

        // 无空元素，添加
        House house = new House();
        house.Address = tbAddress.Text;
        house.Duration = Int32.Parse(tbDuration.Text);
        house.ExpirationDate = DateTime.Parse(tbExpirationDate.Text);
        house.IntakeTime = DateTime.Parse(tbIntakeTime.Text);
        house.Price = float.Parse(tbPrice.Text);
        house.Standard = tbStandard.Text;
        house.RoomNumber = Int32.Parse(tbRoomNumber.Text);

        house.HouseTypeId = this.rblHouseType.SelectedIndex;
        house.LeaseModeId = this.rblLeaseMode.SelectedIndex;
        house.HouseHolderId = this.UserId;

        string liPath = string.Format("~/Images/HouseImages/{0}{1}", DateTime.Now.Ticks,fuldLargeImage.FileName);
        fuldLargeImage.SaveAs(Server.MapPath(liPath));
        house.LargeImageUri = liPath;
        string tnPath = string.Format("~/Images/HouseImages/{0}{1}", DateTime.Now.Ticks,fuldThumbnail.FileName);
        fuldThumbnail.SaveAs(Server.MapPath(tnPath));
        house.ThumbnailUri = tnPath;

        // 地区选择
        house.AreaId = this.ddlAreaList.SelectedIndex;

        house.PublishTime = DateTime.Now;

        // 默认变量
        house.StatusCodeId = 0;
        house.AdminReview = "";
        house.IsChecked = 0;
        house.IsRecommended = 0;
        house.OrderedTimes = 0;
        house.RentedTimes = 0;
        house.Rating = 0;

        // 添加
        if (HouseManage.AddHouse(house) != null)
            // 添加成功， 提示， 跳转
            this.Tip.Text = "添加成功";
        else
            this.Tip.Text = "添加失败";

    }

    protected void AccountChecked()
    {
        if (Session["UserType"] != null && Session["UserId"] != null)
        {
            if (Convert.ToInt32(Session["UserType"]) == 0)
            {
                int id = Convert.ToInt32(Session["UserId"]);
                if (!HouseHolderManage.IsChecked(id))
                {
                    // 跳转 提示页面
                    Response.Redirect("/Error.aspx");
                }
            }
        }
    }
}